package com.example.rk.controller;

import com.example.rk.common.MyPageInfo;
import com.example.rk.common.SessionParameter;
import com.example.rk.entity.Admin;
import com.example.rk.entity.Job;
import com.example.rk.mapper.AdminMapper;
import com.example.rk.service.JobService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/job")
public class JobController {
//    private final  static Logger logger= (Logger) LoggerFactory.getLogger(JobController.class);
    @Autowired
    @Resource
    private JobService service;

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id,HttpServletRequest request) {
        service.deleteById(id);
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        if(admin.getType().equals("学生"))
        {return "redirect:/job/xslist";}
        else{
            return "redirect:/job/list";
        }
    }

    @RequestMapping("/doAdd")
    @ResponseBody
    public String doAdd(Job job,HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        if(admin.getType().equals("学生")){
        job.setSid(admin.getId());}
        return service.doAdd(job);
    }

    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv,HttpServletRequest request) {
        Job job = service.findById(id);
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        mv.addObject("job", job);
        if (admin.getType().equals("管理员")) {
            mv.setViewName("/gly/jobedit");
            return mv;
        } else if (admin.getType().equals("学生")) {
            mv.setViewName("/xs/edit");//不对的之后改
            return mv;
        } else if (admin.getType().equals("公司")) {
            mv.setViewName("/admin/main");
            return mv;
        } else {
            mv.setViewName("/job/add");
            return mv;
        }
    }

    @RequestMapping("/list")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo = service.findBySearch(request);
        //List<RenKo> courseList= renKoService.findAll();
        mv.addObject("searchlocation", request.getParameter("location"));
        mv.addObject("searchposition", request.getParameter("position"));
        mv.addObject("pageInfo", myPageInfo);
        if (admin.getType().equals("管理员")) {
            mv.setViewName("/gly/joblist");
            return mv;
        } else if (admin.getType().equals("学生")) {
            mv.setViewName("/xs/joblist");
            return mv;
        } else if (admin.getType().equals("公司")) {
            mv.setViewName("/gs/joblist");
            return mv;
        } else {
            mv.setViewName("/xs/joblist");
            return mv;
        }
    }
    @RequestMapping("/xslist")
    public ModelAndView xslb(HttpServletRequest request, ModelAndView mv) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        MyPageInfo myPageInfo = service.findBySearch1(request,admin.getId());
        //List<RenKo> courseList= renKoService.findAll();
        mv.addObject("searchlocation", request.getParameter("location"));
        mv.addObject("searchposition", request.getParameter("position"));
        mv.addObject("sid", request.getParameter("sid"));
        mv.addObject("pageInfo", myPageInfo);
        mv.setViewName("/xs/joblist1");
        return mv;

    }
    @RequestMapping("/doEdit")
    @ResponseBody

    public String doEdit(Job job) {
        return service.doEdit(job);
    }
}

